﻿using System;
using Caliburn.Micro;
using DomusDotNet.Sbm.Client.WP7.Services;

namespace DomusDotNet.Sbm.Client.WP7.Framework
{

    public class QueryResult<TResponse> : IResult {

        readonly IQuery<TResponse> query;
        public IBackend Backend { get; set; }
        public TResponse Response { get; set; }


        public QueryResult(IQuery<TResponse> query) {
            this.query = query;
        }

        
        public void Execute(ActionExecutionContext context) {
            Backend.Send(query, response => {
                Response = response;
                Caliburn.Micro.Execute.OnUIThread(() => Completed(this, new ResultCompletionEventArgs()));
            });
        }

        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
    }
}